package com.davidhigenbottam.HeatMap.client;

import java.util.Comparator;

import com.davidhigenbottam.HeatMap.shared.Query;
import com.davidhigenbottam.HeatMap.shared.Province;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class ProvinceTable {
	CellTable<Province> cellTable = new CellTable<Province>(100);
	ListDataProvider<Province> dataProvider = new ListDataProvider<Province>();
	TextColumn<Province> nameColumn;
	TextColumn<Province> valColumn;
	Query query;

	public ProvinceTable() {
		super();

		// Add a text column to show the name.
		nameColumn = new TextColumn<Province>() {
			@Override
			public String getValue(Province p) {
				return p.getName();
			}
		};
		nameColumn.setSortable(true);

		cellTable.addColumn(nameColumn, "Province");

		valColumn = new TextColumn<Province>() {
			@Override
			public String getValue(Province qd) {
				return qd.getValueDescription();
			}
		};
		valColumn.setSortable(true);
		cellTable.addColumn(valColumn, "Value");
		dataProvider.addDataDisplay(cellTable);

		// Add a selection model to handle user selection.
		final SingleSelectionModel<Province> selectionModel = new SingleSelectionModel<Province>();

		cellTable.setSelectionModel(selectionModel);
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						Province qd = selectionModel.getSelectedObject();
						if (qd != null) {
							 runInstitutionQuery(qd.getName());
						}
					}
				});

	}
	
	public void runInstitutionQuery(String provinceName){
//		//Window.alert("You selected: " + provinceName);
//		query.setProvince(provinceName);
//		HeatMap.overlays.runInstitutionQuery(query);
	}

	public void addSortHandlers() {
		ListHandler<Province> nameSortHandler = new ListHandler<Province>(
				getDataProvider().getList());
		nameSortHandler.setComparator(getNameColumn(),
				ProvinceTable.getNameCamparator());
		getCellTable().addColumnSortHandler(nameSortHandler);

		ListHandler<Province> valSortHandler = new ListHandler<Province>(
				getDataProvider().getList());
		valSortHandler.setComparator(getValColumn(),
				ProvinceTable.getValueCamparator());
		getCellTable().addColumnSortHandler(valSortHandler);
	}

	public CellTable<Province> getCellTable() {
		return cellTable;
	}

	public void setCellTable(CellTable<Province> cellTable) {
		this.cellTable = cellTable;
	}

	public ListDataProvider<Province> getDataProvider() {
		return dataProvider;
	}

	public void setDataProvider(ListDataProvider<Province> dataProvider) {
		this.dataProvider = dataProvider;
	}

	private static Comparator<Province> nameCamparator = new Comparator<Province>() {
		public int compare(Province qd1, Province qd2) {
			if (qd1 == qd2) {
				return 0;
			}
			// Compare the name columns.
			if (qd1 != null) {
				return (qd2 != null) ? qd1.getName().compareTo(qd2.getName()) : 1;
			}
			return -1;
		}
	};
	private static Comparator<Province> valueCamparator = new Comparator<Province>() {
		public int compare(Province qd1, Province qd2) {
			if (qd1 == qd2) {
				return 0;
			}
			// Compare the name columns.
			if (qd1 != null) {
				return (qd2 != null) ? Double.valueOf(qd1.getVal()).compareTo(
						Double.valueOf(qd2.getVal())) : 1;
			}
			return -1;
		}
	};

	public TextColumn<Province> getNameColumn() {
		return nameColumn;
	}

	public void setNameColumn(TextColumn<Province> nameColumn) {
		this.nameColumn = nameColumn;
	}

	public TextColumn<Province> getValColumn() {
		return valColumn;
	}

	public void setValColumn(TextColumn<Province> valColumn) {
		this.valColumn = valColumn;
	}

	public static Comparator<Province> getNameCamparator() {
		return nameCamparator;
	}

	public static void setNameCamparator(Comparator<Province> nameCamparator) {
		ProvinceTable.nameCamparator = nameCamparator;
	}

	public static Comparator<Province> getValueCamparator() {
		return valueCamparator;
	}

	public static void setValueCamparator(Comparator<Province> valueCamparator) {
		ProvinceTable.valueCamparator = valueCamparator;
	}

	public void show() {
		cellTable.setVisible(true);
	}

	public void hide() {
		cellTable.setVisible(false);
	}

	public boolean isVisible(){
		return cellTable.isVisible();
	}
	public void setQuery(Query q) {
		this.query= q;
	}

}
